<template>
  <a-drawer
    :title="title"
    width="40%"
    :closable="false"
    :maskClosable="false"
    :visible="visible"
    :body-style="{ paddingBottom: '80px'}"
    @close="onClose"
  >
    <a-tabs v-model="tabkey">
      <a-tab-pane key="1" tab="中文"></a-tab-pane>
      <a-tab-pane key="2" tab="英文" :forceRender="true"></a-tab-pane>
    </a-tabs>
    <a-form-model 
      ref="ruleForm"
      :model="form" 
      :rules="rules"
      :label-col="labelCol" 
      :wrapper-col="wrapperCol" 
      :key="visible"
    >
      <a-form-model-item label="年份" prop="year">
        <a-select
          show-search
          v-model="form.year"
          placeholder="选择年份"
          option-filter-prop="children"
          style="width: 200px"
          :filter-option="(input, option) => option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0"
        >
          <a-select-option :value="i" v-for="(i,index) of yearList" :key="index">{{ i }}</a-select-option>
        </a-select>
      </a-form-model-item> 
      <a-form-model-item label="描述" prop="desc" v-if="tabkey == 1">
        <a-textarea
          v-model="form.desc"
          placeholder="描述"
          :auto-size="{ minRows: 3, maxRows: 5 }"
        />
      </a-form-model-item>
      <a-form-model-item label="英文描述" prop="descEn" v-else>
        <a-textarea
          v-model="form.descEn"
          placeholder="英文描述"
          :auto-size="{ minRows: 3, maxRows: 5 }"
        />
      </a-form-model-item>  
    </a-form-model>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 99999,
      }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="onClose"> 关闭 </a-button>
      <a-button type="primary" @click="onSubmit"> 提交 </a-button>
    </div>
  </a-drawer>
</template>

<script>

import { updateChronicle, addChronicle } from '@/api/manage'
import { showMessage } from '@/utils/mixins'

export default {
  mixins: [showMessage],
  props: {
    parent: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      tabkey: '1',
      title: '',
      visible: false,
      labelCol: {span: 3},
      wrapperCol: {span: 21},
      form: {
        year: undefined,
        desc: undefined,
        descEn: undefined
      },
      rules: {
        year: [{required: true, message: '年份不能为空', trigger: 'change'}],
        desc: [{required: true, message: '描述不能为空', trigger: 'blur'}],
        descEn: [{required: true, message: '描述英文不能为空', trigger: 'blur'}],
      },
      id: undefined,
      status: undefined
    }
  },
  computed: {
    yearList() {
      const _current = new Date().getFullYear()
      const temp = []
      for(let i=_current; i>_current-50; i--) {
        temp.push(i)
      }
      return temp
    }
  },
  methods: {
    // 提交
    onSubmit () {
      this.$refs.ruleForm.validate(vaild => {
        if(vaild) {
          if(this.status == 1) {
            addChronicle(this.form).then(res => {
              this.showMessage(res,() => {
                this.onClose()
                this.parent.getList()
              })
            })
          }else {
            updateChronicle({id:this.id,...this.form}).then(res => {
              if(res.code == '0') {
                this.showMessage(res,() => {
                  this.onClose()
                  this.parent.getList()
                })
              }
            })
          }
        }else {
          return false
        }
      })
    },
    // 关闭
    onClose() {
      this.visible = false
      this.id = undefined
      this.tabkey = '1'
      this.status = undefined
      for(const key in this.form) {
        this.form[key] = undefined
      }
    }
  }
}
</script>

<style lang="less" scoped>
.plus-icon,
.minus-icon {
  font-size: 20px;
  margin-right: 6px;
  float: right;
  color: #F5222D;
  margin-top: 10px;
}
.minus-icon {
  color: #1890ff;
}
</style>